//////////////////////////////////////////////////////////////////////
//
//	Copyright 2010-2011 Rogo's Team. All Rights Reserved
//
//////////////////////////////////////////////////////////////////////
package com.rogo.weibo.platform.netease.api.location
{
	import com.rogo.weibo.core.DoAPI;
	import com.rogo.weibo.platform.netease.api.oauth.Oauth;
	import com.rogo.weibo.utils.HttpParameter;
	import com.rogo.weibo.utils.OauthUrlUtil;
	
	import flash.net.URLRequestMethod;
	
	import json.JSON;
	
	/**
	 * 网易微博API 
	 * 1.1.11 地理位置相关
	 * 
	 * @see http://rogo.sinaapp.com
	 * @see http://open.t.163.com
	 * @author rogo
	 */	
	public class DoLocation extends DoAPI
	{
		public function DoLocation(dataHandler:Function, errorHandler:Function)
		{
			super(dataHandler, errorHandler);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		private static const URL_VENUES:String = 'http://api.t.163.com/location/venues.json';
		public static const CMD_VENUES:String = 'cmdVenues';
		
		private static const URL_LOCATION_TIMELINE:String = 'http://api.t.163.com/statuses/location_timeline.json';
		public static const CMD_LOCATION_TIMELINE:String = 'cmdLocationTimeline';
		
		private static const URL_REPORT_LOCATION:String = 'http://api.t.163.com/location/report.json';
		public static const CMD_REPORT_LOCATION:String = 'cmdReportLocation';
		
		private static const URL_GET_NEIGHBORS:String = 'http://api.t.163.com/location/search_neighbors.json';
		public static const CMD_GET_NEIGHBORS:String = 'cmdGetNeighbors';
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		/**
		 * 上报地址位置
		 * 
		 * @param lat 必选参数，即纬度。范围在+90.0 到 -90.0，如果超出这个范围则返回400
		 * @param long 必选参数,即经度。范围在+180.0到-180.0，如果超出这个范围则返回400
		 */		
		public function reportLocation(lat:String, long:String):void
		{
			var paras:Array = [];
			if (lat != null)
				paras.push(new HttpParameter('lat', lat));
			if (long != null)
				paras.push(new HttpParameter('long', long));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.POST;
			
			paras = OauthUrlUtil.getOauthPostParas(URL_REPORT_LOCATION, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
			var postParam:String = OauthUrlUtil.normalizeRequestParameters(paras);
			
			send(URL_REPORT_LOCATION, 
				resultHandler, 
				errorHandler, 
				URLRequestMethod.POST,
				postParam);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_REPORT_LOCATION, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_REPORT_LOCATION, params);
			}
		}
		
		/**
		 * 获取地点列表
		 * 
		 * @param lat 可选参数，即纬度。范围在+90.0 到 -90.0，如果超出这个范围则忽略该值，必须与long参数一起使用，否则忽略该值
		 * @param long 可选参数,即经度。范围在+180.0到-180.0，如果超出这个范围则忽略该值，必须与lat参数一起使用，否则忽略该值
		 * @param count 可选参数，返回数据的个数，默认为10，最大为50
		 * @param q 可选参数，搜索关键字，必须与经纬度（lat,long）参数同时使用才有效，,根据定位的数据在用户所在的城市搜索相应的POI，
		 * 			如没有经纬度参数或无结果返回404
		 */			
		public function getVenues(lat:String, long:String, count:String, q:String):void
		{
			var paras:Array = [];
			if (lat != null)
				paras.push(new HttpParameter('lat', lat));
			if (long != null)
				paras.push(new HttpParameter('long', long));
			if (count != null)
				paras.push(new HttpParameter('count', count));
			if (q != null)
				paras.push(new HttpParameter('q', q));
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			var url:String = OauthUrlUtil.getOauthUrl(URL_VENUES, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_VENUES, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_VENUES, params);
			}
		}
		
		/**
		 * 获取与地理位置相关的微博列表
		 * 
		 * @param lat 可选参数，即纬度。范围在+90.0 到 -90.0，如果超出这个范围则忽略该值，必须与long参数一起使用，否则忽略该值
		 * @param long 可选参数,即经度。范围在+180.0到-180.0，如果超出这个范围则忽略该值，必须与lat参数一起使用，否则忽略该值
		 * @param vid 可选参数,即POI的id，单独使用时值返回包含此vid的微博。如果经纬度也同时传入，则将两种微博（包含vid或经纬度的）
		 * 			   一起返回，按照发微博的时间倒序排列。如果根据vid找不到相应的poi且无定位参数时则返回404
		 * @param count 可选参数，返回微博的个数，默认为30，最大为200
		 * @param since_id 可选参数，该参数需传cursor_id,返回此条索引之前发的微博列表,不包含此条
		 * @param max_id 可选参数，该参数需传cursor_id,返回此条索引之后发的微博列表，包含此条
		 */		
		public function getLocationTimeline(lat:String, long:String, vid:String, count:String, since_id:String, max_id:String):void
		{
			var paras:Array = [];
			if (lat != null)
				paras.push(new HttpParameter('lat', lat));
			if (long != null)
				paras.push(new HttpParameter('long', long));
			if (vid != null)
				paras.push(new HttpParameter('vid', vid));
			if (count != null)
				paras.push(new HttpParameter('count', count));
			if (since_id != null)
				paras.push(new HttpParameter('since_id', since_id));
			if (max_id != null)
				paras.push(new HttpParameter('max_id', max_id));
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			var url:String = OauthUrlUtil.getOauthUrl(URL_LOCATION_TIMELINE, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_LOCATION_TIMELINE, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_LOCATION_TIMELINE, params);
			}
		}
		
		/**
		 * 获取临近用户
		 * 
		 * @param lat 必选参数，即纬度。范围在+90.0 到 -90.0，如果超出这个范围则返回400
		 * @param long 必选参数,即经度。范围在+180.0到-180.0，如果超出这个范围则返回400
		 */		
		public function getNeighbors(lat:String, long:String):void
		{
			var paras:Array = [];
			if (lat != null)
				paras.push(new HttpParameter('lat', lat));
			if (long != null)
				paras.push(new HttpParameter('long', long));
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			var url:String = OauthUrlUtil.getOauthUrl(URL_GET_NEIGHBORS, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_GET_NEIGHBORS, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_GET_NEIGHBORS, params);
			}
		}
		
		private function resultResponse(cmd:String, params:String):void
		{
			var data:Object = JSON.decode(params);
			_dataHandler(cmd, data);
		}
		
		private function errorResponse(cmd:String, params:String):void
		{
			var data:Object = JSON.decode(params);
			_errorHandler(cmd, data);
		}
	}
}